package com.siyuanren.gcocp.service.impl;

import com.siyuanren.gcocp.common.exception.BaseException;
import com.siyuanren.gcocp.entity.WxCourseStart;
import com.siyuanren.gcocp.entity.WxCourseStartExample;
import com.siyuanren.gcocp.exception.CourseException;
import com.siyuanren.gcocp.mapper.base.WxCourseStartMapper;
import com.siyuanren.gcocp.service.WxCourseStartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by   anxing
 * Project_name gcocp-parent
 * Path         com.siyuanren.gcocp.service.impl
 * Date         2018/6/8
 * Time         16:32
 * Description
 */
@Slf4j
@Service
public class WxCourseStartServiceImpl implements WxCourseStartService {
    @Autowired
    WxCourseStartMapper startMapper;
    @Override
    public WxCourseStart getLatestStart(Integer courseId) throws Exception {
        WxCourseStartExample example = new WxCourseStartExample();
        example.or().andCourseIdEqualTo(courseId).andRegisterLastTimeGreaterThanOrEqualTo(new Date())
                .andAvailableEqualTo(true);
        example.setOrderByClause("register_last_time asc");
        List<WxCourseStart> wxCourseStarts = startMapper.selectByExample(example);
        if (wxCourseStarts.isEmpty()) {
            return null;
        } else {
            return wxCourseStarts.get(0);
        }
    }
}
